package com.hqyj.smvc.handler;

import com.github.pagehelper.PageInfo;
import com.hqyj.smvc.pojo.Company;
import com.hqyj.smvc.pojo.Employee;
import com.hqyj.smvc.pojo.Idcard;
import com.hqyj.smvc.service.CompanyService;
import com.hqyj.smvc.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@Slf4j
public class EmployeeHandler {

    @Autowired
    @Qualifier("employeeService")
    private EmployeeService employeeService;
    @Autowired
    @Qualifier("companyService")
    private CompanyService companyService;

    //查看所有雇员
    @RequestMapping(value = "findAll.action", method = RequestMethod.GET)
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        log.info("Handler>>>findAll,start……");
        //第一次访问第一页
        int page = 1;
        String pageNum = request.getParameter("pageNum");
        //获取访问页码
        if(pageNum != null) {
            page = Integer.parseInt(pageNum);
        }
        //调用service层方法
        List<Employee> employees = employeeService.ShowTheUser(page);
        //分页工具
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);
        System.out.println(pageInfo);
        System.out.println(employees);
        if(employees != null){
            //传入域对象
            request.setAttribute("employeeList", employees);
            request.setAttribute("pageInfo", pageInfo);
            request.getRequestDispatcher("/WEB-INF/employee/findall.jsp").forward(request, response);
        }
        log.info("Handler>>>findAll,end……");
    }

    //解雇雇员
    @RequestMapping(value = "fireEmployee.action", method = RequestMethod.GET)
    public void fireEmployee(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String employee_id = request.getParameter("employee_id");
        int i = Integer.parseInt(employee_id);
        employeeService.fireEmployees(i);
        findAll(request, response);
    }

    //修改雇员跳转页面
    @RequestMapping(value = "updateEmployeeInformationJump.action", method = RequestMethod.GET)
    public String updateEmployeeInformationJump(HttpServletRequest request){
        String employee_id = request.getParameter("employee_id");
        int i = Integer.parseInt(employee_id);
        System.out.println(i);
        Employee employee = employeeService.employeeInformation(i);
        System.out.println(employee);
        request.setAttribute("Information", employee);
        return "/WEB-INF/employee/amendEmployee.jsp";
    }

    //修改雇员
    @RequestMapping(value = "updateEmployeeInformation.action", method = RequestMethod.POST)
    public void updateEmployeeInformation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String employeeId = request.getParameter("employee_id");
        int employee_id = Integer.parseInt(employeeId);
        String employee_name = request.getParameter("employee_name");
        String employee_sex = request.getParameter("employee_sex");
        String employeeAge = request.getParameter("employee_age");
        int employee_age = Integer.parseInt(employeeAge);
        String employee_username = request.getParameter("employee_username");
        String employee_password = request.getParameter("employee_password");
        Employee employee = new Employee();
        employee.setEmployee_id(employee_id);
        employee.setEmployee_name(employee_name);
        employee.setEmployee_sex(employee_sex);
        employee.setEmployee_age(employee_age);
        employee.setEmployee_username(employee_username);
        employee.setEmployee_password(employee_password);
        employeeService.editEmployeeInformation(employee);
        findAll(request, response);
    }

    // 添加跳转
    @RequestMapping(value = "appendEmployeeJump.action", method = RequestMethod.GET)
    public String appendEmployeeJump(HttpServletRequest request){
        List<Company> companies = companyService.checkExistingCompanies();
        request.setAttribute("companies", companies);
        return "/WEB-INF/employee/appendEmployee.jsp";
    }

    // 添加雇员
    @RequestMapping(value = "insertEmployee.action", method = RequestMethod.POST)
    public void insertEmployee(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String employee_name = request.getParameter("employee_name");
        String employee_sex = request.getParameter("employee_sex");
        String employee_age = request.getParameter("employee_age");
        String employee_username = request.getParameter("employee_username");
        String employee_password = request.getParameter("employee_password");
        String company_id = request.getParameter("company_id");
        String company_name = request.getParameter("addCompany_name");
        String company_address = request.getParameter("company_address");
        String card_num = request.getParameter("card_num");
        String card_stat = request.getParameter("card_stat");
        String card_end = request.getParameter("card_end");
        if ((!"".equals(employee_name)) && (!"".equals(employee_age) && ((!"".equals(employee_username)) && (!"".equals(employee_password))))) {
            //封装身份证对象
            Idcard idcard = new Idcard();
            if ((!"".equals(card_num)) && (card_stat != null && card_end != null)) {
                idcard.setIdcard_num(card_num);
                idcard.setIdcard_starttime(card_stat);
                idcard.setIdcard_endtime(card_end);
            }
            //封装公司对象
            Company company = new Company();
            if (company_name != null && company_address != null) {
                company.setCompany_name(company_name);
                company.setCompany_address(company_address);
            } else  if(company_id != null && (!"请选择".equals(company_id))){
                company.setCompany_id(Integer.parseInt(company_id));
            }
            //封装雇员对象
            Employee employee = new Employee();
            employee.setEmployee_name(employee_name);
            employee.setEmployee_sex(employee_sex);
            employee.setEmployee_age(Integer.parseInt(employee_age));
            employee.setEmployee_username(employee_username);
            employee.setEmployee_password(employee_password);
            employee.setCompany(company);
            employee.setIdcard(idcard);
            employeeService.appendEmployee(employee);
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("添加成功！");
        } else {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("请检查雇员信息！");
        }
    }

    //给雇员添加一个新公司
    @RequestMapping(value = "jumpInsertCompany.action", method = RequestMethod.GET)
    public String insertEmployeeCompany(){
        return "/WEB-INF/employee/insertEmployeeCompany.jsp";
    }
}
